package org.design;

import java.util.ArrayList;
import java.util.List;

import org.adapter.SaleAdapter;
import org.cmn.SalePojo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class Sales extends Activity {
	private ListView list;
	private List<SalePojo> listOfSales;
	public static SaleAdapter basicAdapter;

	public static Context currentContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.current_merchant);

		currentContext = this;

		list = (ListView) findViewById(R.id.allSales);
		list.setClickable(true);

		TextView linkInfoMercgant = (TextView) findViewById(R.id.linkInfoSaler);

		listOfSales = new ArrayList<SalePojo>();
		listOfSales.add(new SalePojo("Book", 2, 100));
		listOfSales.add(new SalePojo("Bycicle", 0, 200));
		listOfSales.add(new SalePojo("Car", 0, 300));
		listOfSales.add(new SalePojo("Tablet", 0, 400));
		listOfSales.add(new SalePojo("Phone", 0, 500));
		listOfSales.add(new SalePojo("Movie", 0, 600));
		listOfSales.add(new SalePojo("X-Box", 0, 600));
		listOfSales.add(new SalePojo("Ps", 0, 600));

		basicAdapter = new SaleAdapter(this, listOfSales);

		list.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long index) {
				System.out.println("sadsfsf");
				showToast(listOfSales.get(position).getName());

			}
		});

		list.setAdapter(basicAdapter);

		// ImageView addProductImg = (ImageView) findViewById(R.id.addProducts);
		// addProductImg.setClickable(true);
		//
		// addProductImg.setOnClickListener(new OnClickListener() {
		// public void onClick(View v) {
		// Intent merchantIntent = new Intent(v.getContext(),
		// Products.class);
		// v.getContext().startActivity(merchantIntent);
		// }
		// });

		linkInfoMercgant.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				// this is the code for popup window
				LayoutInflater inflater = (LayoutInflater) Sales.this
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				// Here x is the name of the xml which contains the popup
				// components
				final PopupWindow pw = new PopupWindow(inflater.inflate(
						R.layout.merchant_info_popup, null, false), 200, 300,
						true);
				// Here y is the id of the root component
				pw.showAtLocation(findViewById(R.id.linkInfoSaler),
						Gravity.CENTER, 0, 0);

				Button btnOkPopup = (Button) pw.getContentView().findViewById(
						R.id.btnOkPopup);

				if (btnOkPopup != null) {

					btnOkPopup.setOnClickListener(new OnClickListener() {

						public void onClick(View v) {
							pw.dismiss();
						}
					});
				}
			}
		});
	}

	private void showToast(String message) {
		Toast.makeText(this, message, Toast.LENGTH_LONG).show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.sales_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		switch (item.getItemId()) {
		case R.id.itemid_1:
			Intent loginIntent = new Intent(Sales.currentContext,
					Products.class);
			Sales.currentContext.startActivity(loginIntent);
			return true;
		default:
			return false;

		}
	}
}
